package com.deyuanyun.pic.risk.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.converter.BeanConverter;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.WebUtil;
import com.deyuanyun.pic.risk.controller.vo.RiskAssessmentVO;
import com.deyuanyun.pic.risk.controller.dto.RiskAssessmentQuery;
import com.deyuanyun.pic.risk.domain.RiskAssessment;
import com.deyuanyun.pic.risk.controller.dto.RiskStatisticsQuery;
import com.deyuanyun.pic.risk.service.RiskAssessmentService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 风险评价 控制跳转类
 *
 * @author huanglw
 * @date
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/riskMgr/riskAssessment")
public class RiskAssessmentController {

	@Resource
	RiskAssessmentService riskAssessmentService;

	/**
	 * 跳转到"风险评价"页面
	 *
	 * @return
	 */
	@RequestMapping("/toRiskAssessmentList.html")
	public ModelAndView toRiskAssessmentMgrList() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/riskmgr/riskAssessmentList");
		return mav;
	}

	/**
	 * 根据条件查询风险评价信息
	 *
	 * @return
	 */
	@RequestMapping("/queryRiskAssessment.json")
	@ResponseBody
	public void queryRiskAssessment(RiskAssessmentQuery riskAssessmentQuery) {
		PageJsonBean jsonBean = new PageJsonBean();
		Page<RiskAssessment> riskAssessmentList = riskAssessmentService.queryRiskAssessment(riskAssessmentQuery);
		jsonBean.setList(new RiskAssessmentVO().forDomain(riskAssessmentList));
		AjaxSupport.sendSuccessText("", jsonBean);
	}

	/**
	 * 根据条件导出分析评价信息
	 *
	 * @return
	 */
	@RequestMapping("/export.json")
	@ResponseBody
	public void export(HttpServletResponse response, RiskAssessmentQuery riskAssessmentQuery) throws IOException {

		Page<RiskAssessment> riskAssessmentList = riskAssessmentService.queryRiskAssessment(riskAssessmentQuery);
		List<RiskAssessmentVO> riskAssessmentVOList = new RiskAssessmentVO().forDomain(riskAssessmentList);

		String[] columnKeys = {"code", "pipeLineName", "customNumber", "name", "length",
				"fromMileage", "fromReference", "fromReferenceDistance", "toMileage", "toReference", "toReferenceDistance", "fromLocationDescription", "toLocationDescription",
				"riskaCcroding", "areaLevel", "evaluateMethod", "relativeRiskScore", "damageScore", "corrosionScore",
				"desConstrScore", "geohazardScore", "operationScore", "leadCoefficient", "riskGrade", "mainThreats",
				"riskProposal", "measure", "isControllable", "controlMeasures", "implementDate", "evaluateTime",
				"evaluateCompany", "nextEvaluateTime", "recrder", "recordTime", "remark"};
//		String[] columnNames = {"统一编号", "管道", "管段", "风险段编号", "风险段名称", "风险管段长度（m）",
//				"起点绝对距离（m）", "起点最近参考", "起点相对最近参考点距离（m）", "终点绝对距离（m））", "终点最近参考", "终点相对最近参考点距离（m）",
//				"分段依据", "地区等级", "风险评价方法", "相对风险评分", "第三方破坏评分", "腐蚀评分",
//				"设计与施工缺陷评分", "地质灾害评分", "误操作评分", "泄漏影响系数", "风险等级", "主要威胁因素",
//				"完整性评价建议", "风险减缓措施", " 风险是否已控制到可接受范围之内", "采取的风险控制措施", "实施时间", "风险评价时间",
//				"风险评价单位", "下次风险评价时间", " 记录人", "记录时间", "备注"};
		DownExcelUtil.export("风险评价数据.xls", "/template/excl/risk/risk_template.xls", 2,  riskAssessmentVOList, columnKeys);
	}


	/**
	 * 跳转到"风险评价统计"页面
	 *
	 * @return
     */
	@RequestMapping("/toStatistics.html")
	public ModelAndView toRiskStatistics() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/riskmgr/riskAssessmentStatistics");
		return mav;
	}

	/**
	 * 风险评价统计
	 *
	 * @param riskStatisticsQuery
     */
	@RequestMapping("/statistics.json")
    @ResponseBody
	public void statistics(RiskStatisticsQuery riskStatisticsQuery) {
		AjaxSupport.sendSuccessText("", riskAssessmentService.getRiskStatisticalResults(riskStatisticsQuery));
	}

	/**
	 * 跳转到 高后果区统计 页面
	 *
	 * @return
	 */
	@RequestMapping("/toRiskMap.html")
	public ModelAndView toStatistics(String gisArray) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("gisArray", gisArray);
		mav.setViewName("/riskmgr/riskAssessmentMap");
		return mav;
	}
}
